
package org.ceteca.explica.client.partners;


import java.util.HashSet;

import org.ceteca.explica.client.gui.ExplicaDetailEntityView;
import org.ceteca.explica.client.gui.common.CommonClient;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.common.MultiSelectionSubentityOT;
import org.ceteca.explica.core.common.SelectionSubentityOT;
import org.ceteca.explica.core.partners.ContactOT;
import org.ceteca.explica.core.partners.InterfacePartners;
import org.ceteca.explica.core.partners.VendorOT;
import org.ceteca.explica.core.util.Converter;
import org.ceteca.explica.core.util.OperationResult;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY 
 *         <br>without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
public class VendorClient extends CommonClient {

	// WIDGETS
	private Text entryVendorCode, entryVendorName, entryVendorLastName,
			entryVendorIncomeTax, entryVendorIdNumber;
	
	private Composite panelAddress;
	
	public VendorClient() {
		super(VendorOT.class, "","");
		setEditGladePath(InterfacePartners.EXPLICA_EDIT_VENDOR_GLADE_PATH);
		setDialogName(InterfacePartners.EDIT_VENDOR_DIALOG_NAME);

	}




	@Override
	public BaseVO getEntityData() {

		VendorOT entity = new VendorOT();
		if (this.entEditView != null) {
			this.getWidgets();
			entity.setName(entryVendorName.getText());
			entity.setLastName(entryVendorLastName.getText());
			entity.setIdNumber(entryVendorIdNumber.getText());

			entity.setIncomeTax(Converter.getInstance().parseFloat(
					entryVendorIncomeTax.getText()));

			

			
			if (!isDetail()) {
				String subentityId = InterfacePartners.EXPLICA_ENTITY_ADDRESS_ID;
				entity.clearAddress();
				
				ExplicaDetailEntityView view=this.entEditView.getMultiSelectEntityView(subentityId);
					entity.addAddress(view.getRegistries());
					

			}
			
			if (selectedEntity != null) {
				entity
						.setVersion(((VendorOT) this.selectedEntity)
								.getVersion());
				entity.setIdVendor(((VendorOT) this.selectedEntity)
						.getIdVendor());
			}
		}

		return entity;
	}

	public void getWidgets() {
		entryVendorCode = (Text) this.getWidget(
				InterfacePartners.EDIT_VENDOR_CODE_WIDGETNAME);
		entryVendorName = (Text) this.getWidget(
				InterfacePartners.EDIT_VENDOR_NAME_WIDGETNAME);
		entryVendorLastName = (Text) this.getWidget(
				InterfacePartners.EDIT_VENDOR_LASTNAME_WIDGETNAME);
		entryVendorIdNumber = (Text) this.getWidget(
				InterfacePartners.EDIT_VENDOR_IDNUMBER_WIDGETNAME);
		entryVendorIncomeTax = (Text) this.getWidget(
				InterfacePartners.EDIT_VENDOR_INCOMETAX_WIDGETNAME);
		panelAddress = (Composite)this.getWidget(
				InterfacePartners.EDIT_VENDOR_ADDRESS_PANEL_NAME);
		
		
		
		if (entryVendorCode == null){
			this.logger.error("Error getting entry Vendor Code Widget");
			this.fireExceptionAndExit("");
		}
		if (entryVendorName == null){
			this.logger.error("Error getting entry Vendor Name Widget");
			fireExceptionAndExit("");
		}
		if (entryVendorLastName == null){
			this.logger.error("Error getting entry Vendor Last Name Widget");
			fireExceptionAndExit("");
		}
		if (entryVendorIdNumber == null){
			
			this.logger.error("Error getting entry Vendor Id Number Widget");
			fireExceptionAndExit("");
		}
		if (entryVendorIncomeTax == null){
			fireExceptionAndExit("");
			this.logger.error("Error getting entry Vendor Income Tax Widget");
		}

	}

	public  void setWidgetsValues(BaseVO entity) {

		
		VendorOT ent=(VendorOT)entity;
		getWidgets();
		entryVendorCode.setText(String.format("%d", ent.getIdVendor()));
		entryVendorName.setText(Converter.getInstance().toString(ent.getName()));
		entryVendorLastName.setText(Converter.getInstance().toString(ent.getLastName()));
		entryVendorIdNumber.setText(Converter.getInstance().toString(ent.getIdNumber()));

		entryVendorIncomeTax.setText(Converter.getInstance().toString(
				ent.getIncomeTax()));

		ExplicaDetailEntityView view = this.entEditView.getMultiSelectEntityView(InterfacePartners.EXPLICA_ENTITY_ADDRESS_ID);
		if (view != null) {
			
			view.setRegistries(ent.getAddressSet());
		}else
		{
			this.logger.error("Error. Address subentity view");
			System.exit(-1);
		}
		
		


	}



	@Override
	public OperationResult validateEntity() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setEditionMode(boolean enabled) {
		getWidgets();
		entryVendorCode.setEnabled(false);
		entryVendorIdNumber.setEnabled(enabled);
		entryVendorIncomeTax.setEnabled(enabled);
		entryVendorLastName.setEnabled(enabled);
		entryVendorName.setEnabled(enabled);
	
		
		
		if (entryVendorCode.getText().equals("0"))
			panelAddress.setEnabled(false);
		else
			panelAddress.setEnabled(enabled);

	}



	@Override
	public void buildSelectionSets() {
		lSelectionSubentities = new HashSet<SelectionSubentityOT>();


		lMultiSelectionSubentities = new HashSet<MultiSelectionSubentityOT>();
		lMultiSelectionSubentities.add(new MultiSelectionSubentityOT(
				InterfacePartners.EXPLICA_ENTITY_ADDRESS_ID,
				"treeVendorAddresses",
				this,this.getSwtWidget(InterfacePartners.EDIT_VENDOR_ADDRESS_PANEL_NAME),"addressSet", null));

		
	}

	@Override
	public void init_specific_components() {
		// TODO Auto-generated method stub
		
	}




	@Override
	public void build_extension_fields_view() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void fillEntityData() {
		if (this.selectedEntity != null && this.entEditView != null) {
			getWidgets();
			setWidgetsValues((VendorOT) selectedEntity);
		}
	}
}
